package com.hs.android.fortress.ingame;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.hs.android.fortress.model.Bomb;

public class ExplostionAnimation {

	private GoogleMap mMap;
	private ExplosionCallback mCallback;
	private Activity mActivity;

	public ExplostionAnimation(Activity activity, GoogleMap map) {
		mMap = map;
		mActivity = activity;
	}

	public void setCallback(ExplosionCallback callback) {
		mCallback = callback;
	}

	public interface ExplosionCallback {
		public void callback();
	}

	public void startAnimation(LatLng exPosition, Bomb bomb, boolean isMine) {

		new animationThread(exPosition, bomb, isMine).start();
	}

	private class animationThread extends Thread {

		private LatLng position;
		private Bomb bomb;
		private boolean isMine;

		private Circle fCircle;

		private int fRange;

		public animationThread(LatLng pos, Bomb b, boolean mine) {
			position = pos;
			bomb = b;
			isMine = mine;

			fRange = bomb.getExplosionRange();
		}

		@Override
		public void run() {

			mActivity.runOnUiThread(new Runnable() {

				@Override
				public void run() {
					fCircle = mMap.addCircle(explosionCircleOpts(position,
							fRange, Color.RED));
				}
			});

			while (fRange > 1) {
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				mActivity.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						if(fRange > 0){
							fCircle.setRadius(fRange);
							fRange--;
						}
					}
				});
			}

			mActivity.runOnUiThread(new Runnable() {

				@Override
				public void run() {
					fCircle.remove();
				}
			});

			if (isMine && mCallback != null)
				mCallback.callback();
		}

		private CircleOptions explosionCircleOpts(LatLng pos, int range,
				int color) {
			return new CircleOptions().center(pos).radius(range)
					.strokeWidth(0.0f).fillColor(color);
		}

	}

}
